package com.sskj.fabi.presenter;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.bean.OrderRecordBean;
import com.sskj.fabi.http.HttpConfig;
import com.sskj.fabi.ui.fragment.FabiRecordFragment;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;


public class FabiRecordFragmentPresenter extends BasePresenter<FabiRecordFragment> {
    public Flowable<List<OrderRecordBean>> getData(String page, String pageSize) {
        return OkGo.<HttpData<PageBean<OrderRecordBean>>>get(HttpConfig.BASE_URL + HttpConfig.FABI_ORDER_RECORD)
                .params("page", page)
                .params("size", pageSize)
                .converter(new JsonConverter<HttpData<PageBean<OrderRecordBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(haiPageBeanHttpData -> haiPageBeanHttpData.getData().getList())
                .onErrorReturnItem(new ArrayList<>());
    }


    /**
     * 标记已付款
     *
     * @param orderNum
     */
    public void markPay( String orderNum) {
        httpService.markPay( orderNum)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (response.body().getStatus() == 200) {
                           mView.refresh();
                        }
                    }
                });
    }

    /**
     * 取消订单
     *
     * @param orderNum
     */
    public void cancelOrder(String orderNum) {
        httpService.cancelOrder(orderNum)
                .execute(new JsonCallBack<HttpData>(this) {

                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (response.body().getStatus() == 200) {
                            mView.refresh();

                        }
                    }
                });
    }

    /**
     * 账单申诉
     *
     * @param orderNum
     */
    public void allegeOrder(String orderNum, String refer, String shop_account, String ss_type, String ss_reason) {
        httpService.allegeOrder(orderNum, refer, shop_account, ss_type, ss_reason)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (response.body().getStatus() == 200) {
                            mView.refresh();

                        }
                    }
                });
    }

    /**
     * 确认放行
     *
     * @param orderNum
     * @param pwd
     */
    public void letGo(String orderNum, String pwd) {
        httpService.letGo(orderNum, pwd)
                .execute(new JsonCallBack<HttpData>(this) {

                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());

                        if (response.body().getStatus() == 200) {
                            mView.refresh();

                        }
                    }
                });
    }

}
